package it.uniroma2.art.coda.ssexample;

import it.uniroma2.art.coda.interfaces.SmartSuggestionInterface;
import it.uniroma2.art.coda.structures.SmartSuggestionStruct;

public class SmartSuggExample extends SmartSuggestionInterface{
	
	public SmartSuggExample(String id, String description) {
		super(id, description);
	}

	public SmartSuggestionStruct interrogateComponent(){
		SmartSuggestionStruct smartSuggStruct = new SmartSuggestionStruct();
		
		
		
		return smartSuggStruct;
	}
	
	/*
	public CodaFinalSuggestions interrogateComponent2() {
		CodaFinalSuggestions codaFinalSuggs;
		//List <ProjectionRule> projRuleDependendList = new ArrayList<ProjectionRule>();
		codaFinalSuggs = new CodaFinalSuggestions();
		//Annotation ann = casAnn.getAnnotation();
		
		try{
			//all the structure from the other component or from the previous suggestion
			List <ProjectionRule> selProjRuleList = selProjRules.getProjRuleList();
			
			for(int i=0; i<selProjRuleList.size(); ++i){
				//analyze each triple in the graph section of the rule to construct a suggestion (triple)
				ProjectionRule projRule = selProjRuleList.get(i);
				double confidence = projRule.getConfidenceValue();
				Collection <GraphStruct>graphList = projRule.getGrapgList();
				for(GraphStruct graph : graphList){
					if(graph.hasIssue())
						continue;
					GraphSingleElement subjGSE = graph.getSubj();
					GraphSingleElement predGSE = graph.getPred();
					GraphSingleElement objGSE = graph.getObj();
					
					List<ARTNode> subjList = getARTNodeFromGraphElem(subjGSE, codaFinalSuggs);
					List<ARTNode> predList = getARTNodeFromGraphElem(predGSE, codaFinalSuggs);
					List<ARTNode> objList  = getARTNodeFromGraphElem(objGSE, codaFinalSuggs);
					//now add the triple with all the information taken from the structures
					for(ARTNode subj : subjList){
						for(ARTNode pred : predList){
							for(ARTNode obj : objList){
								if(subj == null || pred == null || obj == null)
									continue;
								ARTTriple artTriple = new ARTTriple(subj, pred, obj, confidence);
								codaFinalSuggs.addTriple(artTriple);
							}
						}
					}
				}
			}
		} catch (ModelAccessException e) {
			e.printStackTrace();
		}
		
		return codaFinalSuggs;
	}
	
	
	private List<ARTNode> getARTNodeFromGraphElem(GraphSingleElement graphElem, CodaFinalSuggestions codaFinalSuggs) throws ModelAccessException{
		List<ARTNode> artNode = new ArrayList<ARTNode>();
		
		Map <NodeStruct, ARTURIResource> bindLabelMap = bindLabels.getLabelToARTURIResMap();
		List <PreviousDecisionSingleElement> prevDecList = prevDecision.getPrevDecSingleElemList();
		
		if(graphElem instanceof GraphSingleElemUri){
			GraphSingleElemUri graphElemUri = (GraphSingleElemUri) graphElem;
			artNode.add(rdfModel.createURIResource(rdfModel.expandQName(graphElemUri.getURI())));
		}
		else if(graphElem instanceof GraphSingleElemAlias){
			//TODO add the support for the variable
			return artNode; // artNode = null;
		}
		else if(graphElem instanceof GraphSingleElemNode){
			GraphSingleElemNode graphElemNode = (GraphSingleElemNode)graphElem;
			if(graphElemNode.hasExternalReference()){ // external reference
				String otherProjRuleId = graphElemNode.getOtherPRRuleId();
				ProjectionRule otherProjRule = prModel.getProjRuleFromId(otherProjRuleId);
				if(otherProjRule == null)
					return artNode; // artNode = null;
				String uimaTypeOfOtherProjectionRule = otherProjRule.getUIMAType();
				Map <String, String> paramsMap = graphElem.getPRRule().getParameterssMap();
				String key = "previousUpTo_"+otherProjRuleId; 
				String uimaTypeAnnToStop = paramsMap.get(key);	//uimaTypeAnnToStop is the type system of the annoation to which
																// this reference as to stop (can't go any further)
				boolean stop = false;
				int prevDecPos = prevDecList.size()-1;
				while(stop == false){
					PreviousDecisionSingleElement prevDecSingleElem = prevDecList.get(prevDecPos--);
					String typeNamePrevAnn = prevDecSingleElem.getsuggOntCoda().getAnnotation().getType().getName();
					if(typeNamePrevAnn.compareTo(uimaTypeAnnToStop) == 0 || prevDecPos == -1)
						stop = true;
					if(typeNamePrevAnn.compareTo(uimaTypeOfOtherProjectionRule) == 0){
						ARTNode artNodeSingle = prevDecSingleElem.getsuggOntCoda().getCodaFinalSuggs().
								getARTNodeFromLabel(otherProjRule, graphElemNode.getNodeId());
						if(artNodeSingle != null)
							artNode.add(artNodeSingle);
					}
				}
				
			}
			else{ // it is defined in the same projection rule
				NodeStruct node = graphElemNode.getNode();
				ARTURIResource artUriRes = bindLabelMap.get(node);
				if(artUriRes != null){
					artNode.add(artUriRes);
					codaFinalSuggs.addLabelToARTNode(graphElemNode.getPRRule(), node.getName(), artUriRes);
				}
				else{
					String type = node.getType();
					String label = node.getName();
					ARTNode artNodeSingle = codaFinalSuggs.getARTNodeFromLabel(graphElemNode.getPRRule(), label);
					if(artNodeSingle != null){
						artNode.add(artNodeSingle);
						return artNode;
					}
					//artNode == null
					String value = codaCore.getValueOfFeature(casAnn.getAnnotation(), node);
					if(type.compareTo("uri") == 0){
						artNode.add(rdfModel.createURIResource(rdfModel.getBaseURI()+"#"+value));
						codaFinalSuggs.addLabelToARTNode(graphElemNode.getPRRule(), node.getName(), artNode.get(0));
					}
					else if(type.compareTo("plainLiteral") == 0){
						artNode.add(rdfModel.createLiteral(value));
						codaFinalSuggs.addLabelToARTNode(graphElemNode.getPRRule(), node.getName(), artNode.get(0));
					}
					else if(type.startsWith("literal")){
						String typeOfLiteral = type.split("\\(")[1].split("\\)")[0];
						ARTURIResource typedLiteral = rdfModel.createURIResource(rdfModel.expandQName(typeOfLiteral)); //TODO check this part
						artNode.add(rdfModel.createLiteral(value, typedLiteral));
					}
					else
						return null;
				}
			}
		}
		return artNode;
	}*/

}

